#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
#import colorsys
from PIL import  Image
import colorsys
import optparse
def get_main_color(image):
    # 转换颜色模式，输入RGB值
    source_image = Image.open(image)
    image = source_image .convert('RGBA')

    # 生成缩略图，减少计算量
    #image.thumbnail((200,200))
    max_score = 0
    main_color = None
    # for count, (r, g, b, a) in image.getcolors(image.size[0]*image.size[1]):
    #     if count < max_score:
    #         continue
    #     else:
    #         max_score = count
    #         main_color = (r, g, b, a)
    #
    # return main_color
    for count, (r, g, b, a) in image.getcolors(image.size[0]*image.size[1]):
        if count < max_score:
            continue
        else:
            max_score = count
            main_color = (r, g, b, a)

    return main_color
        # # 跳过纯黑色
        # if a == 0:
        #     continue
        # saturation = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)[1]
        # y = min(abs(r*2014 + g*4130 + b*802 + 4096 + 131072) >> 13, 235)
        # y = (y-16.0) / (235-16)
        #
        # # 忽略高亮色
        # if y > 0.9:
        #     continue
        #
        # score = (saturation + 0.1) * count
        # if score > max_score:
        #     max_score = score
        #     dominant_color = (r, g, b)
        # return dominant_color

